package org.yzh.web.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.yzh.commons.util.PageResultDTO;
import org.yzh.commons.util.StrUtils;
import org.yzh.web.entity.ElectronicFence;
import org.yzh.web.entity.PersonnelAlarmEntity;
import org.yzh.web.mapper.ElectronicFenceMapper;
import org.yzh.web.mapper.PersonnelAlarmMapper;
import org.yzh.web.request.PersonnelAlarmAddRequest;
import org.yzh.web.request.PersonnelAlarmPageSearchRequest;
import org.yzh.web.response.PersonnelAlarmPageSearchResponse;
import org.yzh.web.service.ElectronicFenceService;
import org.yzh.web.service.PersonnelAlarmService;

import javax.annotation.Resource;
import java.time.LocalDateTime;


@Service
public class PersonnelAlarmServiceImpl extends ServiceImpl<PersonnelAlarmMapper, PersonnelAlarmEntity> implements PersonnelAlarmService {

    @Resource
    private PersonnelAlarmMapper personnelAlarmMapper;
    @Override
    public void save1(PersonnelAlarmAddRequest personnelAlarmAddRequest) {
        PersonnelAlarmEntity personnelAlarmEntity = new PersonnelAlarmEntity();
        BeanUtils.copyProperties(personnelAlarmAddRequest,personnelAlarmEntity);
        personnelAlarmEntity.setGmtCreate(LocalDateTime.now());
        personnelAlarmEntity.setGmtModified(LocalDateTime.now());
        personnelAlarmEntity.setIsValid(1);
        personnelAlarmMapper.save1(personnelAlarmEntity);
    }

    @Override
    public void update(PersonnelAlarmAddRequest personnelAlarmAddRequest) {
        PersonnelAlarmEntity personnelAlarmEntity = new PersonnelAlarmEntity();
        BeanUtils.copyProperties(personnelAlarmAddRequest,personnelAlarmEntity);
        personnelAlarmEntity.setGmtModified(LocalDateTime.now());
        personnelAlarmMapper.update(personnelAlarmEntity);
    }

    @Override
    public void delete(Long id) {

    }

    @Override
    public PageResultDTO<PersonnelAlarmPageSearchResponse> select(PersonnelAlarmPageSearchRequest request) {

        LambdaQueryWrapper<PersonnelAlarmEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PersonnelAlarmEntity::getIsValid, 1);
        if (request.getId() != null) {
            queryWrapper.like(PersonnelAlarmEntity::getId, request.getId());
        }
        if (StrUtils.isNotBlank( request.getAlarmType())) {
            queryWrapper.like(PersonnelAlarmEntity::getAlarmType, request.getAlarmType());
        }
        if (StrUtils.isNotBlank( request.getDomainName())) {
            queryWrapper.like(PersonnelAlarmEntity::getDomainName, request.getDomainName());
        }

        queryWrapper.orderByAsc(PersonnelAlarmEntity::getSerialNumber);
//        Page<PersonnelAlarmEntity> of = Page.of(request.getPageNum(), request.getPageSize());
//        Page<PersonnelAlarmEntity> page = this.page(of);
        Page<PersonnelAlarmEntity> selectPage = this.baseMapper.selectPage(new Page<>(request.getPageNum(), request.getPageSize()), queryWrapper);
        if (selectPage.getRecords()
                .isEmpty()) {
            return PageResultDTO.empty();
        }

        return PageResultDTO.convert(selectPage, PersonnelAlarmPageSearchResponse::convert);
    }
    }


